<?php

class editSector extends bee\form
{protected static $__formID = "frm200911271608";
  public $__caption = "Edit sector data";
  public $__version = "1.0";
  protected $__activeMenu = "sectorOverview";

  protected $error;
  protected $sector;
  protected $arrCustomer;

  protected $tID;
  protected $tName;
  protected $tDescription;
  protected $oCustomer;


  public function createForm($evt)
  { $this->sector = new sector();
    if(isset($evt["id"]))
    { $this->sector->SELECT($evt["id"]);
    }
    $this->tID           = new bee\text("id", $this->sector->id);
    $this->tName         = new bee\text("name", $this->sector->name);
    $this->tDescription  = new bee\text("description", $this->sector->description);


    $customer = new bee\org_unit();
    $rsCustomer = new bee\rowSet($customer);
    $this->arrCustomer = $rsCustomer->getList("name", "id");

    $this->oCustomer = new bee\option("org_id", $this->sector->org_id, $this->arrCustomer, "name");


  }

  public function loadForm($evt)
  { $this->error = new bee\serror();
  }

  public function drawForm($evt)
  { static::$DSDoc->setRow($this->sector);

  }


  public function add($evt)
  { $this->sector->fillFromArray($evt);
    if($this->validate($this->sector, $this->error))
    { $this->sector->org_id = bee\app::$user->org_id;
      $this->sector->INSERT();
      bee\app::setMessage("Sector successfully added.");
    } else
    { bee\app::setMessage($this->error->err_description, MSG_LEVEL_ERROR);
    }
  }

  public function edit($evt)
  { $this->sector->fillFromArray($evt);
    if($this->validate($this->sector, $this->error))
    { $this->sector->UPDATE();
      bee\app::setMessage("Sector dats successfully modified.");
    } else
    { bee\app::setMessage($this->error->err_description, MSG_LEVEL_ERROR);
    }
  }

  public function delete($evt)
  { $client = new user_ext();
    $client->sector = $this->sector->id;
    $rsClient = new bee\rowSet($client);
    if($rsClient->getRow($client))
    { bee\app::setMessage("Sector \"" . $this->sector->name . "\" can't be deleted becase is in use.", MSG_LEVEL_WARNING);
    } else
    { $this->sector->DELETE();
      bee\app::setMessage("Sector \"" . $this->sector->name . "\" successfully deleted.");
    }
    header("Location: index.php?__form=" . $this->__parent . "&sys_msg=" . urlencode(bee\app::getMessageText()) . "&sys_msg_style=" . bee\app::getMessageLevel());
  }

  protected function validate(sector $row, bee\serror &$error)
  { if(!$row->name)
    { $error->err_code = -10;
      $error->err_description .= "\nSector name must be given.";
      return false;
    }
    $error->err_code = 0;
    return true;
  }
}
